﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using Microsoft.Office.Core;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using Entity;
using DAL;
using Entity;
using System.Data;
using System.Threading;

namespace Project_V1
{
    public class DataSync
    { 
        /// <summary>
        /// List转CSV
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="fullPath"></param>
    public static void SavePowerOffValueToCSV( string fullPath)
        {
            try
            { 
                string[] headers = {   // CommonMethods.CurrentName["MaxFeedingTime"],
                                        CommonMethods.CurrentName["_03_HMI.FeedMachRivetOutOverTime"],
                                        CommonMethods.CurrentName["_03_HMI.EnableVacuoValveLater_SampleTime1"],
                                        CommonMethods.CurrentName["_03_HMI.EnableVacuoValveLater_SampleTime2"],
                                        CommonMethods.CurrentName["_03_HMI.VacuoRchLater_SampleTime"],
                                        CommonMethods.CurrentName["_03_HMI.CachePlaceRivetInOverTime"],
                                        CommonMethods.CurrentName["_03_HMI.CachePlaceRivetAtHeartbeatTime"], 
                                        CommonMethods.CurrentName["_03_HMI.CachePlaceRivetOutOverTime"],
                                        CommonMethods.CurrentName["_03_HMI.GunHeadRivetInOverTime"],
                                        CommonMethods.CurrentName["_03_HMI.GunHeadRivetIn_EnableVacuoValveDelayTime"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_InhaleRivetMaxTime"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_InhaleRivetIntervalTime"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_InhaleRivetMaxCounter"],
                                        CommonMethods.CurrentName["_03_HMI.PrePressureSetValue"], 
                                        CommonMethods.CurrentName["_03_HMI.FeedPos1"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_InhaleRivetTargetPos"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Velo1"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Acc1"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Dec1"], 
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Jerk1"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Velo2"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Acc2"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Dec2"],
                                        CommonMethods.CurrentName["_03_HMI.FeedTest_Jerk2"],
                                        CommonMethods.CurrentName["_03_HMI.CachePlaceRivetAtOverTime"],
                                        CommonMethods.CurrentName["_03_HMI.FeedMachModel"],
                                        CommonMethods.CurrentName["_03_HMI.settingsId"]

                };
                string line = "";
                string data = "";
                FileInfo fi = new FileInfo(fullPath);
                if (!fi.Directory.Exists)
                {
                    fi.Directory.Create();
                }
                else
                {
                  

                }
                try
                {
                    // 确保文件存在
                    if (File.Exists(fullPath))
                    {
                        // 删除文件
                        File.Delete(fullPath); 
                    }
                    else
                    { 
                    }
                }
                catch (Exception ex)
                {
                    //Console.WriteLine("无法删除文件：" + ex.Message);
                }



                FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                var header = string.Empty;

                line = string.Join(",", headers);
                sw.WriteLine(line);
                //写出各行数据 
                line = "";

                line += //CommonMethods.CurrentValue["MaxFeedingTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedMachRivetOutOverTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_SampleTime1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_SampleTime2"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.VacuoRchLater_SampleTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.CachePlaceRivetInOverTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.CachePlaceRivetAtHeartbeatTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.CachePlaceRivetOutOverTime"] + "," +
                        CommonMethods.CurrentValue["_03_HMI.GunHeadRivetInOverTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.GunHeadRivetIn_EnableVacuoValveDelayTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_InhaleRivetMaxTime"] + "," +
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_InhaleRivetIntervalTime"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_InhaleRivetMaxCounter"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.PrePressureSetValue"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedPos1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_InhaleRivetTargetPos"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Velo1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Acc1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Dec1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Jerk1"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Velo2"] + "," +  
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Acc2"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Dec2"] + "," + 
                        CommonMethods.CurrentValue["_03_HMI.FeedTest_Jerk2"] + "," +
                        CommonMethods.CurrentValue["_03_HMI.CachePlaceRivetAtOverTime"] + "," + CommonMethods.CurrentValue["_03_HMI.FeedMachModel"] + "," + CommonMethods.CurrentValue["_03_HMI.settingsId"];

                ///settingsId
                sw.WriteLine(line);
                sw.Close();
                fs.Close();
                //MessageBox.Show("导出成功\r\n导出文件位置\r\nc:\\工艺参数.csv");
            }
            catch (Exception ex)
            { 
            }
        }
          
        /// <summary>
        /// List转CSV
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="fullPath"></param>
        public static void WritePowerOffValueToPLC(string fullPath)
        {
            try
            { 
                string path = fullPath; // 替换为你的CSV文件路径
                using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamReader reader = new StreamReader(fs))
                {
                    string[] names = reader.ReadLine().Split(',');
                    string[] values = reader.ReadLine().Split(',');
                    string temptype = "";
                    if (names.Length > 0)
                    {
                   
                        for (int i = 0; i < names.Length; i++)
                        {
                   
                            names[i] =   names[i];
                            temptype = CommonMethods.CurrentType[names[i]].ToLower();

                          
                            if (names[i] == "_03_HMI.settingsId") { 
                                FrmView.RunSettingsid = values[i];
                                continue;
                            }

                            try
                            {
                                if (CommonMethods.CurrentType[names[i]].ToLower() == "bool")
                                {
                                    Beckhoff_PLC.IsWriting = true;
                                    if (values[i].ToLower() == "false")
                                    {
                                        Beckhoff_PLC.WriteVariable(names[i], false);
                                    }
                                    if (values[i].ToLower() == "true")
                                    {
                                        Beckhoff_PLC.WriteVariable(names[i], true);
                                    }
                                    Beckhoff_PLC.IsWriting = false;
                                }
                            }
                            catch (Exception ex)
                            {
                                int j = 0;
                            }


                            Beckhoff_PLC.IsWriting = true;
                                Thread.Sleep(20);

                            try
                            {
                                if (CommonMethods.CurrentType[names[i]].ToLower() == "float")
                                {
                                    if (Beckhoff_PLC.WriteVariable(names[i], float.Parse(values[i].Trim())))//将每个变量的变量名传来,在Tag中
                                    {

                                    }
                                    else
                                    {

                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                               
                            }



                            try
                            { 
                                if (CommonMethods.CurrentType[names[i]].ToLower() == "ushort" || CommonMethods.CurrentType[names[i]].ToLower() == "unsigned")
                                {
    


                                    if (Beckhoff_PLC.WriteVariable(names[i], ushort.Parse(values[i].Trim())))//将每个变量的变量名传来,在Tag中
                                    {

                                    }
                                    else
                                    {

                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                
                            }


                            try
                            {
                                if (CommonMethods.CurrentType[names[i]].ToLower() == "int")
                                {
                                    if (Beckhoff_PLC.WriteVariable(names[i], Int16.Parse(values[i].Trim())))//将每个变量的变量名传来,在Tag中
                                    {

                                    }
                                    else
                                    {

                                    }
                                }

                            }
                            catch (Exception ex)
                            {
                                int j = 0;
                            }

                            try
                            {
                                if (CommonMethods.CurrentType[names[i]].ToLower() == "uint32" || CommonMethods.CurrentType[names[i]].ToLower() == "unsigned32")
                                {
                                    if (Beckhoff_PLC.WriteVariable(names[i], UInt32.Parse(values[i].Trim())))//将每个变量的变量名传来,在Tag中
                                    {

                                    }
                                    else
                                    {

                                    }
                                }
                            }
                            catch (Exception ex)
                            { 
                            }
                            Beckhoff_PLC.IsWriting = false;

                    
                        }

            






                    }

                }
                 
            }
            catch (Exception ex)
            {


                int j = 0;
            }
        }



       
































}
}
